package com.example.security1.util;

import com.example.security1.util.vo.RouterVO;
import com.example.security1.web.menu.entity.Menu;
import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

/**
 * 菜单树
 */
public class MakeMenuTree {
    public static List<Menu> makeTree(List<Menu> menuList, Long pid) {
        ArrayList<Menu> list = new ArrayList<>();
        Optional.ofNullable(menuList)
                .orElse(new ArrayList<>())
                .stream()
                .filter(item -> item != null && item.getParentId().equals(pid))
                .forEach(item -> {
                    Menu menu = new Menu();
                    BeanUtils.copyProperties(item, menu);
                    // 递归查找下级
                    List<Menu> children = makeTree(menuList, item.getPId());
                    menu.setChildren(children);
                    list.add(menu);
                });
        return list;
    }

    public static List<RouterVO> makeRouter(List<Menu> menuList, Long pid) {
        ArrayList<RouterVO> list = new ArrayList<>();
        Optional.ofNullable(menuList)
                .orElse(new ArrayList<>())
                .stream()
                .filter(item -> item != null && Objects.equals(item.getParentId(), pid))
                .forEach(item -> {
                    RouterVO router = new RouterVO();
                    router.setName(item.getName());
                    router.setPath(item.getPath());
                    // 设置children，每一项的下一级
                    List<RouterVO> children = makeRouter(menuList, item.getPId());
                    router.setChildren(children);
                    // parent为0时，为一级菜单，component为Layout
                    if (item.getParentId() == 0L) {
                        router.setComponent("Layout");
                        // 判断一级菜单是否为次阿迪娜类型，0 目录 1 菜单
                        if (item.getType().equals("1")) {
                            // 菜单
                            // 一级菜单，设置redirect转发到他的children
                            router.setRedirect(item.getPath());
                            // 如果一级菜单属于菜单类型，需要设置它的children
                            List<RouterVO> listChildren = new ArrayList<>();
                            // 构造children
                            RouterVO child = new RouterVO();
                            child.setName(item.getName());
                            child.setPath(item.getPath());
                            child.setComponent(item.getUrl());
                            child.setMeta(new RouterVO.Meta(
                                    item.getTitle(),
                                    item.getIcon(),
                                    item.getCode().split(",")
                            ));
                            listChildren.add(child);
                            router.setChildren(listChildren);
                            router.setPath(item.getPath() + "parent");
                            router.setName(item.getName() + "parent");
                        }
                    } else {
                        router.setComponent(item.getUrl());
                    }
                    // 设置meta
                    router.setMeta(new RouterVO.Meta(
                            item.getTitle(),
                            item.getIcon(),
                            item.getCode().split(",")
                    ));
                    list.add(router);
                });
        return list;
    }
}
